<template>
	<view class="chat">
		<!-- 顶部标题 -->
		<view class="topTabbar" v-if="false">
			<!-- 返回图标 -->
			<u-icon class="icon" name="arrow-left" size="20px" color="#000" @click="goback()"></u-icon>
			<!-- 标题 -->
			<view class="text">匿名</view>
		</view>
		<scroll-view :style="{height: `${windowHeight-inputHeight - 80}rpx`}" id="scrollview" scroll-y="true"
			:scroll-top="scrollTop" class="scroll-view">
			<!-- 聊天主体 -->
			<view id="msglistview" class="chat-body">
				<!-- 聊天记录 -->
				<view v-for="(item,index) in msgList" :key="index">
					<!-- 自己发的消息 -->
					<view class="item self" v-if="item.userContent != ''">
						<!-- 文字内容 -->
						<view class="content right">
							{{item.userContent}}
						</view>
						<!-- 头像 -->
						<image class="avatar" :src="item.image">
						</image>
					</view>
					<!-- 机器人发的消息 -->
					<view class="item Ai" v-if="item.botContent != ''">
						<!-- 头像 -->
						<image class="avatar" :src="item.image">
						</image>
						<!-- 文字内容 -->
						<view class="content left">
							{{item.botContent}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部消息发送栏 -->
		<!-- 用来占位，防止聊天消息被发送框遮挡 -->
		<view class="chat-bottom" :style="{height: `${inputHeight}rpx`}">
			<view class="send-msg" :style="{bottom:`${keyboardHeight - 60}rpx`}">
				<view class="uni-textarea">
					<textarea v-model="chatMsg" maxlength="300" confirm-type="send" @confirm="handleSend"
						placeholder="请输入咨询内容~" :show-confirm-bar="false" :adjust-position="false" @linechange="sendHeight"
						@focus="focus" @blur="blur" auto-height></textarea>
				</view>
				<button @click="handleSend" class="send-btn">发送</button>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//键盘高度
				keyboardHeight: 0,
				//底部消息发送高度
				bottomHeight: 0,
				//滚动距离
				scrollTop: 0,
				userId: '',
				//发送的消息
				chatMsg: "",
				msgList: [{
						botContent: "你好啊，很高兴你可以关注我，请问我有什么可以帮助你的吗？",
						userContent: "",
						image: "/static/user.png"
					},
					{
						botContent: "",
						userContent: "你好呀，非常高兴认识你",
						image: "/static/wx.png"
					},
				],
				commentList:[],  //获取的评论列表
			    commentValue:'',  //用户评论内容
			    isSocketOpen:false,  //socket是否打开
			    pingpangTimes:null,  //socket心跳计时器
				$baseUrl: 'https://baidu.com',
				lid: 123
			}
		},
		updated() {
			//页面更新时调用聊天消息定位到最底部
			this.scrollToBottom();
		},
		computed: {
			windowHeight() {
				return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
			},
			// 键盘弹起来的高度+发送框高度
			inputHeight() {
				return this.bottomHeight + this.keyboardHeight
			}
		},
		onLoad() {
			uni.onKeyboardHeightChange(res => {
				//这里正常来讲代码直接写
				//this.keyboardHeight=this.rpxTopx(res.height)就行了
				//但是之前界面ui设计聊天框的高度有点高,为了不让键盘和聊天输入框之间距离差太大所以我改动了一下。
				this.keyboardHeight = this.rpxTopx(res.height)
				if (this.keyboardHeight < 0) this.keyboardHeight = 0;
			})
			
			// this.chatInit()  //socket相关操作
		},
		beforeDestroy() {
			clearInterval(this.pingpangTimes)  //清除socket心跳定时器
			// uni.closeSocket()  //关闭socket
		},
		onUnload() {
			// uni.offKeyboardHeightChange()
		},
		methods: {
			chatInit(){
				uni.request({
					url: 'http://47.100.32.115:8080/jeecgboot/api/colorpath/nailTag/querylist',  //此接口返回socket请求地址
					method: 'GET'
				}).then((res)=>{
					let socketlink=''
					if(this.userToken){  //已登录，携带token
						socketlink=`${res.data.msg}?token=${this.userToken}`
					}else{  //未登录
						socketlink=res.data.msg
					}
					this.commentList=[]  //创建新的socket连接前先清除之前的实时聊天记录
					uni.closeSocket()  //创建新的socket连接前确保旧的已关闭
					//创建一个socket连接
					uni.connectSocket({
						url:encodeURI(socketlink),  //这里建议使用encodeURI编码，以解决ios上可能出现的连接兼容问题（猜测socketlink里存在中文、空格、特殊字符或link字符串过长时可能会在ios上出现连接问题）
					})
					//监听socket打开
					uni.onSocketOpen(()=>{
						this.isSocketOpen=true
						console.log('WebSocket连接已打开！');
					})
					//监听socket关闭
					uni.onSocketClose(()=>{
						this.isSocketOpen=false
						console.log('WebSocket连接已关闭！');
					})
					//监听socket错误
					uni.onSocketError(()=>{
						this.isSocketOpen=false
						console.log('WebSocket连接打开失败');
					})
					//监听socket消息
					uni.onSocketMessage((res)=>{
						return this.getComment(res)
					})
							
					//先确保清除了之前的心跳定时器
					clearInterval(this.pingpangTimes)
					//每过一段时间发送一次心跳，发送Ping,服务器会反馈pong，防止断连
					this.pingpangTimes=setInterval(()=>{
						uni.sendSocketMessage({
							data: "ping",
							success:()=>{},
							fail:()=>{
								this.isSocketOpen=false
							}
						});
					},60000)
				})
				console.log('----------')
			},
			//监听socket消息
			getComment(res){
			  let infos=JSON.parse(res.data)  //socket信息是字符串，需要先转成json形式再去解析内容
			  //后端规定cadmin为0--用户发言，为1--管理员发言，为2--系统提示，为3--需登录，为5--心跳信息
			  if(typeof(infos.cadmin)=="undefined"){  //infos.cadmin不存在说明返回的是数组，说明是第一次获取的之前的所有聊天记录
				this.commentList=this.commentList.concat(infos)
				//设置定时器，到时间后滚动到最底部。使用scroll-view组件的scroll-top属性。只要scroll-top的值大于滚动区域的最大高度即可实现滚动到最底部
				let scrolltimes=setTimeout(()=>{  
				  this.commentScrollCount=this.commentList.length*200
				  clearTimeout(scrolltimes)
				},600)
		
			  //后续每次更新一条有效的新聊天（心跳信息pong不是有效聊天），就会触发这个
			  }else if(infos.cadmin==0||infos.cadmin==1||infos.cadmin==2){
				this.commentList=this.commentList.concat(infos)
				let scrolltimes=setTimeout(()=>{
				  this.commentScrollCount=this.commentList.length*200
				  clearTimeout(scrolltimes)
				},150)
			  }else if(infos.cadmin==3){  //未登录
				this.loginPopRemind="您尚未登录或您的登录已过期，请重新登录后发言"
				uni.removeStorageSync('kusername')
				uni.removeStorageSync('kuserid')
				uni.removeStorageSync('kuserimg')
				uni.removeStorageSync('kusertoken')
				this.$refs.noLoginPopup.open()
			  }
			},	
			//发表评论
			sendComment(val){
				if(val==""){
					uni.showToast({title: '说点什么吧 ~',icon:'none'})
				}else if(val.length>300){
					uni.showToast({title: `评论字数请勿超过300个字符，当前 ${val.length}`,icon:'none'})
				}else{
					if(this.userToken){  //已登录
						if(this.isSocketOpen){  //socket连接正常
							this.commentValue=val
							let theValue={
								"ccontent":this.commentValue
							}
							let transedValue=JSON.stringify(theValue)   //后端规定的评论数据格式:json转字符串
								uni.sendSocketMessage({
									data: transedValue,
									success:()=>{
										this.commentValue=""
										uni.showToast({title: '发送成功',icon:'none'})
									},
									fail:()=>{
										uni.showToast({title: '发送失败，请稍后再试或重新进入此页面尝试',icon:'none'})
									}
								});
						}else{  //socket已断开
							uni.showToast({title: '聊天断开啦，请重新进入此页面尝试 ~',icon:'none'})
						}
					}else{  //未登录
						this.$refs.needLoginPopup.open()
					}
				}
			},
			goback() {
				uni.switchTab({
					url: "/pages/tutorship/tutorship"
				})
			},
			focus() {
				this.scrollToBottom()
			},
			blur() {
				this.scrollToBottom()
			},
			// px转换成rpx
			rpxTopx(px) {
				let deviceWidth = uni.getSystemInfoSync().windowWidth
				let rpx = (750 / deviceWidth) * Number(px)
				return Math.floor(rpx)
			},
			// 监视聊天发送栏高度
			sendHeight() {
				setTimeout(() => {
					let query = uni.createSelectorQuery();
					query.select('.send-msg').boundingClientRect()
					query.exec(res => {
						this.bottomHeight = this.rpxTopx(res[0].height)
					})
				}, 10)
			},
			// 滚动至聊天底部
			scrollToBottom(e) {
				setTimeout(() => {
					let query = uni.createSelectorQuery().in(this);
					query.select('#scrollview').boundingClientRect();
					query.select('#msglistview').boundingClientRect();
					query.exec((res) => {
						if (res[1].height > res[0].height) {
							this.scrollTop = this.rpxTopx(res[1].height - res[0].height)
						}
					})
				}, 15)
			},
			// 发送消息
			handleSend() {
				//如果消息不为空
				if (!this.chatMsg || !/^\s+$/.test(this.chatMsg)) {
					let obj = {
						botContent: "",
						userContent: this.chatMsg,
						image: "/static/tell_icon.png"
					}
					this.msgList.push(obj);
					this.chatMsg = '';
					this.scrollToBottom()
				} else {
					this.$modal.showToast('不能发送空白消息')
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	$chatContentbgc: #C2DCFF;
	$sendBtnbgc: #4F7DF5;

	view,
	button,
	text,
	input,
	textarea {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	/* 聊天消息 */
	.chat {
		.topTabbar {
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			display: flex;
			margin-top: 80rpx;
			justify-content: space-between;

			.icon {
				margin-left: 20rpx;
			}

			.text {
				margin: auto;
				font-size: 16px;
				font-weight: 700;
			}

			.button {
				width: 10%;
				margin: auto 20rpx auto 0rpx;
			}
		}

		.scroll-view {
			::-webkit-scrollbar {
				display: none;
				width: 0 !important;
				height: 0 !important;
				-webkit-appearance: none;
				background: transparent;
				color: transparent;
			}

			// background-color: orange;
			background-color: #F6F6F6;

			.chat-body {
				display: flex;
				flex-direction: column;
				padding-top: 23rpx;
				// background-color:skyblue;

				.self {
					justify-content: flex-end;
				}

				.item {
					display: flex;
					padding: 23rpx 30rpx;
					// background-color: greenyellow;

					.right {
						background-color: $chatContentbgc;
					}

					.left {
						background-color: #FFFFFF;
					}

					// 聊天消息的三角形
					.right::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						left: 100%;
						top: 10px;
						border: 12rpx solid transparent;
						border-left: 12rpx solid $chatContentbgc;
					}

					.left::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						top: 10px;
						right: 100%;
						border: 12rpx solid transparent;
						border-right: 12rpx solid #FFFFFF;
					}

					.content {
						position: relative;
						max-width: 486rpx;
						border-radius: 8rpx;
						word-wrap: break-word;
						padding: 24rpx 24rpx;
						margin: 0 24rpx;
						border-radius: 5px;
						font-size: 32rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
						line-height: 42rpx;
					}

					.avatar {
						display: flex;
						justify-content: center;
						width: 78rpx;
						height: 78rpx;
						background: $sendBtnbgc;
						border-radius: 50rpx;
						overflow: hidden;

						image {
							align-self: center;
						}

					}
				}
			}
		}

		/* 底部聊天发送栏 */
		.chat-bottom {
			width: 100%;
			height: 100rpx;
			background: #F4F5F7;
			transition: all 0.1s ease;

			.send-msg {
				display: flex;
				align-items: flex-end;
				padding: 16rpx 30rpx;
				width: 100%;
				min-height: 177rpx;
				position: fixed;
				bottom: 0;
				background: #fff;
				transition: all 0.1s ease;
			}

			.uni-textarea {
				padding-bottom: 70rpx;

				textarea {
					width: 537rpx;
					min-height: 75rpx;
					max-height: 500rpx;
					background: #f1f1f1;
					border-radius: 40rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					color: #333333;
					line-height: 74rpx;
					padding: 5rpx 8rpx;
					text-indent: 30rpx;
				}
			}

			.send-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 76rpx;
				margin-left: 25rpx;
				width: 120rpx;
				height: 75rpx;
				background: #ed5a65;
				border-radius: 50rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 28rpx;
			}
		}
	}
</style>